<?php


class CollectionResourceTable extends Doctrine_Table {

    public static function getInstance() {
        return Doctrine_Core::getTable('CollectionResource');
    }

    public function createLazyQuery() {
        $q = Doctrine_Query::create();
        $q->select('r.uin, r.CollectionUin, r.ResourceName, r.ResourceLink, '.
                'r.RequiresIngest, r.OriginalFilename, r.ResourceSize, r.Base64Encoded');
        $q->from('CollectionResource r');
        $q->addWhere('deleted_at is null');
        return $q;
    }

    public function addFindByCollectionAndResourceName($collectionUin,$resourceName, Doctrine_Query $q = null) {
        if ($q == null) {
            $q  = $this->createLazyQuery();
        }
        $q->addWhere('UPPER(resourceName) = ?',array(strtoupper($resourceName)));
        $q->addWhere('collectionUin = ?',$collectionUin);

        return $q;
    }

    public function addFindByCollection($collectionUin, Doctrine_Query $q = null) {
        if ($q == null) {
            $q  = $this->createLazyQuery();
        }
        $q->addWhere('collectionUin = ?',$collectionUin);

        return $q;
    }

    public static function getResourceUploadDir() {
        return sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.'resources';
    }

    public function ingestResourcesFromFileSystem() {
        /**
         * Find all resources requiring ingestion
         */

        $q = $this->createQuery();
        $q->addWhere('requiresIngest = true');

        $resourcesToIngest = $q->execute();

        foreach($resourcesToIngest as $resourceToIngest) {
            if (file_exists($this->getResourceUploadDir().DIRECTORY_SEPARATOR.$resourceToIngest->getResourceLink())) {
                /**
                 * TODO: Base64 encoding seems to be required for postgres despite
                 * the fact that its supposed to be a binary object ....
                 * FIX ME
                 */
                if (sfConfig::get('app_resources_base64_encode_resources')) {
                    $resourceToIngest->setResourceData(base64_encode(file_get_contents($this->getResourceUploadDir().DIRECTORY_SEPARATOR.$resourceToIngest->getResourceLink())));
                    $resourceToIngest->setBase64Encoded(true);
                } else {
                    $resourceToIngest->setResourceData(file_get_contents($this->getResourceUploadDir().DIRECTORY_SEPARATOR.$resourceToIngest->getResourceLink()));
                    $resourceToIngest->setBase64Encoded(false);
                }
                $resourceToIngest->setResourceSize(filesize($this->getResourceUploadDir().DIRECTORY_SEPARATOR.$resourceToIngest->getResourceLink()));
                $resourceToIngest->setRequiresIngest(false);
                $resourceToIngest->save();
                unlink($this->getResourceUploadDir().DIRECTORY_SEPARATOR.$resourceToIngest->getResourceLink());
                unset($resourceToIngest);
            }
        }
    }
}